package com.jbp.controller;

import com.jbp.exception.RRException;
import com.jbp.model.LuckKey;
import com.jbp.model.WalletExchange;
import com.jbp.service.LuckKeyService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Map;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 钥匙(LuckKey)表控制层
 * @author cc
 * @since 2021-04-29 17:54:46
 */
@Api(tags ="APP接口--LuckKey" )
@RestController
@RequestMapping("rest/luckKey")
public class LuckKeyController {
    @Autowired
    private LuckKeyService luckKeyService;

    /** 获取锁的数量 */
    @GetMapping("/num")
    public R list(@RequestAttribute("userId")Integer userId){
        LuckKey luckKey = luckKeyService.getBuyUserId(userId);
        return R.ok().put("data", luckKey==null ? 0 : luckKey.getNum());
    }

    ///购买
    @PostMapping("/buy")
    public synchronized R buy(@RequestAttribute("userId")Integer userId, @RequestBody LuckKey luckKey){
        if (luckKey.getNum()==null) {
            return R.error("参数异常");
        }
        if (luckKey.getNum().intValue()<1) {
            throw new RRException("参数异常");
        }
        luckKeyService.buy(userId, luckKey);
        return R.ok();
    }
}